import { createRouter, createWebHistory } from 'vue-router'
import { useEnterpriseStore } from '../stores/enterprise'
import { getInfoStatus } from '@/api/info';
import { ElMessage } from 'element-plus';

const RESTRICTED_ROUTES = [
    'home',
    'report-list',
    'report-form',
    'search',
    'notice-list',
    'notice-detail'
];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            redirect: "/home",
        },
        {
            path: "/login",
            name: "login",
            component: () => import("../views/LoginView.vue"),
        },
        {
            path: '/',
            component: () => import('../layouts/MainLayout.vue'),
            children: [
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('../views/HomeView.vue'),
                },
                {
                    // 报表列表页面（Dashboard）
                    path: 'profile',
                    name: 'profile',
                    component: () => import('../views/ProfileView.vue'),
                },
                {
                    // 报表列表页面（Dashboard）
                    path: 'report/list',
                    name: 'report-list',
                    component: () => import('../views/ReportListView.vue'),
                },
                {
                    // 报表填写/编辑页面
                    path: 'report/form',
                    name: 'report-form',
                    component: () => import('../views/ReportFormView.vue'),
                },
                {
                    path: 'search',
                    name: 'search',
                    component: () => import('../views/ReportSearchView.vue'),
                },
                {
                    // 通知列表页面
                    path: 'notice/list',
                    name: 'notice-list',
                    component: () => import('../views/NoticeListView.vue'),
                    meta: {
                        title: '通知中心'
                    }
                },
                {
                    // 通知详情页面
                    path: 'notice/detail/:id',
                    name: 'notice-detail',
                    component: () => import('../views/NoticeDetailView.vue'),
                    meta: {
                        title: '通知详情'
                    }
                },
                {
                    path: 'unregistered',
                    name: 'unregisterd',
                    component: () => import('../views/UnregisteredView.vue'),
                    meta: {
                        title: '未备案信息'
                    }
                }
            ]
        }
    ],
});

// 不需要进行备案验证的路由路径
const WHITE_LIST = ['/login', '/profile'];

// 全局前置守卫，用于验证企业备案状态
router.beforeEach(async (to, from, next) => {
  // 对于白名单路由，直接放行
  if (WHITE_LIST.includes(to.path)) {
    next();
    return;
  }

  const enterpriseStore = useEnterpriseStore();
  
  // 如果还没有检查过企业备案状态，则进行检查
  if (!enterpriseStore.isStatusChecked) {
    await enterpriseStore.fetchProfileStatus();
  }
  
  // 如果企业未通过备案（状态不为3），则强制跳转到profile页面进行备案
  if (!enterpriseStore.isProfileApproved) {
    next('/profile');
    return;
  }
  
  // 企业已通过备案，正常访问请求的页面
  next();
});

export default router